import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sph_driver/Main/FleetManagement/FleetRoadView.dart';
import 'package:sph_driver/Main/FleetManagement/AddTrailerCardView.dart';

import '../../AppConfig/Config.dart';
import '../../AppConfig/RouteAct.dart';
import '../../AppConfig/StringEncapsulation.dart';
import '../../http/NoLoadingGetApi.dart';
import '../EditInformation/EditCarCard.dart';
import 'AddVehicleView.dart';

class FleetInformationView extends StatefulWidget {
  const FleetInformationView({super.key, required this.fleetMap, required this.id});
  final Map fleetMap;
  final String id;
  @override
  State<FleetInformationView> createState() => _FleetInformationViewState();
}

class _FleetInformationViewState extends State<FleetInformationView> {
  Map vehicleMap={};
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if(widget.id!="")
    getCarMessage();
  }
  void getCarMessage(){
    NoLoadinggetApiManager.getData(
      context,
      "api/auth/driver/vehicle/detail",
      {'id':widget.id,},
          (responseData)  {
        setState(() {
          vehicleMap=responseData['data'];

        });

      },
    );
  }
  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: Color.fromRGBO(248, 248, 248, 1),
      appBar: AppBar(
        backgroundColor: Color.fromRGBO(231, 112, 27, 0.85),
        title: Text("添加车辆",style: TextStyle(color: Colors.white,fontWeight: FontWeight.w300),),
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios, color: Colors.white),
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
      body:  Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            colors: [MyColors.themeColor,MyColors.backgreyColor, MyColors.backgreyColor,MyColors.backgreyColor], // 渐变颜色数组
            begin: Alignment.topCenter, // 渐变开始的位置
            end: Alignment.bottomCenter, // 渐变结束的位置
          ),

        ),
        child:
        ListView(
          children: [
            Container(
              width: screenSize.width ,
              child: Column(
                children: [
                  Row(
                    children: [
                      SizedBox(width: 20.w,),
                      Container(
                        width: 60.w,
                        height: 60.h,
                        child:  widget.fleetMap['avatarPath']!=null&&widget.fleetMap['avatarPath']!=""?
                        CircleAvatar(
                          radius: 15,
                          backgroundImage: NetworkImage(MyServiceUrl.imgHeader +widget.fleetMap['avatarPath']),
                        )
                            :  CircleAvatar(
                          radius: 15,
                          backgroundImage: AssetImage("lib/Images/login/默认头像.jpeg"),
                        ),
                      ),
                      SizedBox(width: 8.w,),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(widget.fleetMap['name'],style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 16.sp),),
                          if(widget.fleetMap['description']!=null)
                            Text(widget.fleetMap['description'].toString(),style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 16.sp),),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ),
            SizedBox(height: 8.h,),
            GestureDetector(
              onTap: ()async{
                final result=await Navigator.push( context, FadeRoute(page:AddVehicleView(dataMap: vehicleMap,)));
                if(result!=null){
                  getCarMessage();
                }
              },
              child: Container(
                margin: EdgeInsets.only(left: 10.w,right: 10.w),
                padding: EdgeInsets.only(right: 12.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(10)),
                ),
                child: Column(
                  children: [
                    SizedBox(height: 12.h,),
                    vehicleMap['vehicleDrivingLicenseInfo']!=null?
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            Text("    "+"行驶证信息",style: TextStyle(color: Colors.black,fontWeight: FontWeight.w600,fontSize: 14.sp),),
                            SizedBox(width: 4.w,),
                            if(compareDateWithCurrent(vehicleMap['vehicleDrivingLicenseInfo']['expiryDate'])=="2"||compareDateWithCurrent(vehicleMap['vehicleDrivingLicenseInfo']['expiryDate'])=="3")
                              BrnTagCustom.buildBorderTag(
                                tagText: '即将过期',
                                textColor: Colors.red,
                                borderColor: Colors.red,
                                borderWidth: 1,
                                fontSize: 9.sp,
                                textPadding: EdgeInsets.all(1),
                              ),
                            if(compareDateWithCurrent(vehicleMap['vehicleDrivingLicenseInfo']['expiryDate'])=="4")
                              BrnTagCustom.buildBorderTag(
                                tagText: '已过期',
                                textColor: Colors.red,
                                borderColor: Colors.red,
                                borderWidth: 1,
                                fontSize: 9.sp,
                                textPadding: EdgeInsets.all(1),
                              ),
                            SizedBox(width: 4.w,),
                            if(vehicleMap['vehicleDrivingLicenseStatus']== 1)
                              BrnTagCustom.buildBorderTag(
                                tagText: '审核中',
                                textColor: Colors.red,
                                borderColor: Colors.red,
                                borderWidth: 1,
                                fontSize: 9.sp,
                                textPadding: EdgeInsets.all(1),
                              ),
                          ],
                        ),

                        GestureDetector(
                          child: Text("编辑",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),
                          onTap: () async {
                            final result=await Navigator.push( context, FadeRoute(page:AddVehicleView(dataMap: vehicleMap,)));
                            if(result=="success"){

                            }else if(result=="Navigator"){
                              final result=await Navigator.push( context, FadeRoute(page:AddTrailerCardView(dataMap: vehicleMap,)));

                              if(result!=null){
                                getCarMessage();
                              }
                            }
                          },
                        )
                      ],
                    ):
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Text("    "+"行驶证信息",style: TextStyle(color: Colors.black,fontWeight: FontWeight.w600,fontSize: 14.sp),),
                      ],
                    ),
                    SizedBox(height: 12.h,),
                    Divider(color: Color.fromRGBO(240, 240, 240, 1),),
                    SizedBox(height: 12.h,),
                    vehicleMap['vehicleDrivingLicenseInfo']!=null?
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Text("    "+vehicleMap['vehicleDrivingLicenseInfo']['vehicleNum'],style: TextStyle(color: Colors.black,fontSize: 14.sp,),),
                      ],
                    ) :
                    GestureDetector(
                      child: Container(
                        width: 150.w,
                        height: 35.h,
                        decoration: BoxDecoration(
                          color: Color.fromRGBO(231, 112, 27, 0.85),
                          borderRadius: BorderRadius.all(Radius.circular(60)),
                        ),
                        child:Center(
                          child:Text("+"+"添加行驶证",style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 14.sp),),
                        ),
                      ),
                      onTap: ()async{
                        final result=await Navigator.push( context, FadeRoute(page:AddVehicleView(dataMap: vehicleMap,)));
                        if(result=="success"){
                          getCarMessage();
                        }else if(result=="Navigator"){
                          final result=await Navigator.push( context, FadeRoute(page:AddTrailerCardView(dataMap: vehicleMap,)));

                          if(result!=null){
                            getCarMessage();
                          }
                        }
                      },
                    ),

                    SizedBox(height: 12.h,),
                  ],
                ),
              ),
            ),

            if(vehicleMap['vehicleDrivingLicenseInfo']!=null&&vehicleMap['vehicleDrivingLicenseInfo']['vehicleType'].contains("挂"))

              SizedBox(height: 8.h,),
            if(vehicleMap['vehicleDrivingLicenseInfo']!=null&&vehicleMap['vehicleDrivingLicenseInfo']['vehicleType'].contains("挂"))
            GestureDetector(
              onTap: ()async{
                final result=await Navigator.push( context, FadeRoute(page:AddTrailerCardView(dataMap: vehicleMap,)));
                if(result!=null){
                  getCarMessage();
                }
              },
              child: Container(
                margin: EdgeInsets.only(left: 10.w,right: 10.w),
                padding: EdgeInsets.only(right: 12.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(10)),
                ),
                child: Column(
                  children: [
                    SizedBox(height: 12.h,),
                    vehicleMap['trailerDrivingLicenseInfo']!=null&&vehicleMap['trailerDrivingLicenseInfo']['scrapDate']!=null?
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            Text("    "+"行驶证信息",style: TextStyle(color: Colors.black,fontWeight: FontWeight.w600,fontSize: 14.sp),),
                            SizedBox(width: 4.w,),
                            if(compareDateWithCurrent(vehicleMap['trailerDrivingLicenseInfo']['scrapDate'])=="2"||compareDateWithCurrent(vehicleMap['trailerDrivingLicenseInfo']['scrapDate'])=="3")
                              BrnTagCustom.buildBorderTag(
                                tagText: '即将过期',
                                textColor: Colors.red,
                                borderColor: Colors.red,
                                borderWidth: 1,
                                fontSize: 9.sp,
                                textPadding: EdgeInsets.all(1),
                              ),
                            if(compareDateWithCurrent(vehicleMap['trailerDrivingLicenseInfo']['scrapDate'])=="4")
                              BrnTagCustom.buildBorderTag(
                                tagText: '已过期',
                                textColor: Colors.red,
                                borderColor: Colors.red,
                                borderWidth: 1,
                                fontSize: 9.sp,
                                textPadding: EdgeInsets.all(1),
                              ),
                            SizedBox(width: 4.w,),
                            if(vehicleMap['trailerDrivingLicenseStatus']== 1)
                              BrnTagCustom.buildBorderTag(
                                tagText: '审核中',
                                textColor: Colors.red,
                                borderColor: Colors.red,
                                borderWidth: 1,
                                fontSize: 9.sp,
                                textPadding: EdgeInsets.all(1),
                              ),
                          ],
                        ),
                        //
                        GestureDetector(
                          child: Text("编辑",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),
                          onTap: () async {
                            final result=await Navigator.push( context, FadeRoute(page:AddVehicleView(dataMap: vehicleMap,)));
                            if(result!=null){
                              getCarMessage();
                            }
                          },
                        )
                      ],
                    )
                    :
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Text("    "+"挂车行驶证信息",style: TextStyle(color: Colors.black,fontWeight: FontWeight.w600,fontSize: 14.sp),),
                      ],
                    ),
                    SizedBox(height: 12.h,),
                    Divider(color: Color.fromRGBO(240, 240, 240, 1),),
                    SizedBox(height: 12.h,),
                    vehicleMap['trailerDrivingLicenseInfo']!=null&&vehicleMap['trailerDrivingLicenseInfo']['vehicleNum']!=null?
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Text("    "+vehicleMap['trailerDrivingLicenseInfo']['vehicleNum'],style: TextStyle(color: Colors.black,fontSize: 14.sp,),),
                      ],
                    ) :
                    GestureDetector(
                      child: Container(
                        width: 150.w,
                        height: 35.h,
                        decoration: BoxDecoration(
                          color: Color.fromRGBO(231, 112, 27, 0.85),
                          borderRadius: BorderRadius.all(Radius.circular(60)),
                        ),
                        child:Center(
                          child:Text("+"+"添加挂车行驶证",style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 14.sp),),
                        ),
                      ),
                      onTap: ()async{
                        final result=await Navigator.push( context, FadeRoute(page:AddTrailerCardView(dataMap: vehicleMap,)));
                        if(result!=null){
                          getCarMessage();
                        }
                      },
                    ),

                    SizedBox(height: 12.h,),
                  ],
                ),
              ),
            ),
            SizedBox(height: 8.h,),
            GestureDetector(
              onTap: ()async{
                final result=await Navigator.push( context, FadeRoute(page:FleetRoadView(dataMap: vehicleMap,)));
                if(result!=null){
                  getCarMessage();
                }
              },
              child: Container(
                margin: EdgeInsets.only(left: 10.w,right: 10.w),
                padding: EdgeInsets.only(right: 12.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(10)),
                ),
                child: Column(
                  children: [
                    SizedBox(height: 12.h,),
                    vehicleMap['roadTransportCertificateInfo']!=null&&vehicleMap['roadTransportCertificateInfo']['expiryDate']!=null?
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            Text("    "+"道路运输证",style: TextStyle(color: Colors.black,fontWeight: FontWeight.w600,fontSize: 14.sp),),
                            SizedBox(width: 4.w,),
                            if(compareDateWithCurrent(vehicleMap['roadTransportCertificateInfo']['expiryDate'])=="2"||compareDateWithCurrent(vehicleMap['roadTransportCertificateInfo']['expiryDate'])=="3")
                              BrnTagCustom.buildBorderTag(
                                tagText: '即将过期',
                                textColor: Colors.red,
                                borderColor: Colors.red,
                                borderWidth: 1,
                                fontSize: 9.sp,
                                textPadding: EdgeInsets.all(1),
                              ),
                            if(compareDateWithCurrent(vehicleMap['roadTransportCertificateInfo']['expiryDate'])=="4")
                              BrnTagCustom.buildBorderTag(
                                tagText: '已过期',
                                textColor: Colors.red,
                                borderColor: Colors.red,
                                borderWidth: 1,
                                fontSize: 9.sp,
                                textPadding: EdgeInsets.all(1),
                              ),
                            SizedBox(width: 4.w,),
                            if(vehicleMap['roadTransportCertificateStatus']== 1)
                              BrnTagCustom.buildBorderTag(
                                tagText: '审核中',
                                textColor: Colors.red,
                                borderColor: Colors.red,
                                borderWidth: 1,
                                fontSize: 9.sp,
                                textPadding: EdgeInsets.all(1),
                              ),
                          ],
                        ),

                        GestureDetector(
                          child: Text("编辑",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),
                          onTap: () async {
                            final result=await Navigator.push( context, FadeRoute(page:FleetRoadView(dataMap: vehicleMap,)));
                            if(result!=null){
                              getCarMessage();
                            }
                          },
                        )
                      ],
                    ):
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Text("    "+"道路运输证证信息",style: TextStyle(color: Colors.black,fontWeight: FontWeight.w600,fontSize: 14.sp),),
                      ],
                    ),
                    SizedBox(height: 12.h,),
                    Divider(color: Color.fromRGBO(240, 240, 240, 1),),
                    SizedBox(height: 12.h,),
                    vehicleMap['roadTransportCertificateInfo']!=null&&vehicleMap['roadTransportCertificateInfo']['vehicleNum']!=null?
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Text("    "+vehicleMap['roadTransportCertificateInfo']['vehicleNum'],style: TextStyle(color: Colors.black,fontSize: 14.sp,),),
                      ],
                    ) :
                    GestureDetector(
                      child: Container(
                        width: 150.w,
                        height: 35.h,
                        decoration: BoxDecoration(
                          color: Color.fromRGBO(231, 112, 27, 0.85),
                          borderRadius: BorderRadius.all(Radius.circular(60)),
                        ),
                        child:Center(
                          child:Text("+"+"添加道路运输证",style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 14.sp),),
                        ),
                      ),
                      onTap: ()async{
                        final result=await Navigator.push( context, FadeRoute(page:FleetRoadView(dataMap: vehicleMap,)));
                        if(result!=null){
                          getCarMessage();
                        }
                      },
                    ),

                    SizedBox(height: 12.h,),
                  ],
                ),
              ),
            ),


          ],

        ),
      ),
    );
  }
}
